<?php

namespace addons\tools\library\ali\aip;

class AipSpeech extends AipBase
{

    /**
     *
     * @param $bizParams
     */
    public function synthesis($action, $bizParams)
    {
        $config = $this->uploadConfig($action);
        $params = array(
            "AccessKeyId"      => $this->accessKeyID,
            "SignatureMethod"  => "HMAC-SHA1",
            "SignatureNonce"   => uniqid(),
            "SignatureVersion" => "1.0",
            "Timestamp"        => gmdate(self::DF),
            "Format"           => "JSON",
            "RegionId"         => "cn-shanghai",
            "Version"          => $config['Version'],
            'Action'           => $action
        );
        unset($bizParams['tool_id']);
        if (isset($config['Params'])) {
            $bizParams[$config['Params']] = $bizParams['ImageURL'];
            unset($bizParams['ImageURL']);
        }
        if (!empty($bizParams)) {
            $params = array_merge($params, $bizParams);
        }
        unset($params["Signature"]);
        ksort($params);
        // 3. 构造待签名的字符串
        $sortedQueryStringTmp = "";
        foreach ($params as $key => $value) {
            $sortedQueryStringTmp .= "&" . $this->specialUrlEncode($key) . "=" . $this->specialUrlEncode($value);
        }
        $sortedQueryString = substr($sortedQueryStringTmp, 1);// 去除第一个多余的&符号
//         4. 签名最后也要做特殊URL编码
        $stringToSign = $this->apiHttpMethod . "&" . $this->specialUrlEncode("/") . "&" . $this->specialUrlEncode($sortedQueryString);
        $signature = $this->specialUrlEncode($this->sign($this->accessKeySecret . "&", $stringToSign));
        // 5、最终生成出合法请求的URL
        $requestUrl = 'https://' . $config['Parent'] . '.' . self::EndPoint . "/?Signature=" . $signature . $sortedQueryStringTmp;
        return $this->request($requestUrl, []);
    }
}